package com.diseaseInformation

class DiseaseController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    telemedicine.MyformatToHtmlService myformatToHtmlService
    
    def index = {
        redirect(action: "list", params: params)
    }    
    
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [diseaseInstanceList: Disease.list(params), diseaseInstanceTotal: Disease.count()]
    }

    def create = {
        def diseaseInstance = new Disease()
        diseaseInstance.properties = params
        return [diseaseInstance: diseaseInstance]
    }

    def save = {
        def diseaseInstance = new Disease(params)

        if (diseaseInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'disease.label', default: 'Disease'), diseaseInstance.id])}"
            redirect(action: "show", id: diseaseInstance.id)
        }
        else {
            render(view: "create", model: [diseaseInstance: diseaseInstance])
        }
    }
    
    def show = {
        myformatToHtmlService.diseaseviewverinc(params.id)
        def diseaseInstance = Disease.read(params.id)
        if (!diseaseInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'disease.label', default: 'Disease'), params.id])}"
            redirect(action: "list")
        }
        else {
            diseaseInstance.data = myformatToHtmlService.toHtmlTagWebboard(diseaseInstance.data)
            Disease.get(params.id).viewer++//Count User Viewver
            [diseaseInstance: diseaseInstance]
        }
    }

    def edit = {
        def diseaseInstance = Disease.get(params.id)
        if (!diseaseInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'disease.label', default: 'Disease'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [diseaseInstance: diseaseInstance]
        }
    }

    def update = {
        def diseaseInstance = Disease.get(params.id)
        if (diseaseInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (diseaseInstance.version > version) {
                    
                    diseaseInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'disease.label', default: 'Disease')] as Object[], "Another user has updated this Disease while you were editing")
                    render(view: "edit", model: [diseaseInstance: diseaseInstance])
                    return
                }
            }
            diseaseInstance.properties = params
            if (!diseaseInstance.hasErrors() && diseaseInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'disease.label', default: 'Disease'), diseaseInstance.id])}"
                redirect(action: "show", id: diseaseInstance.id)
            }
            else {
                render(view: "edit", model: [diseaseInstance: diseaseInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'disease.label', default: 'Disease'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def diseaseInstance = Disease.get(params.id)
        if (diseaseInstance) {
            try {
                diseaseInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'disease.label', default: 'Disease'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'disease.label', default: 'Disease'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'disease.label', default: 'Disease'), params.id])}"
            redirect(action: "list")
        }
    }
    
    
}
